/**
 * Created by quliang on 16-4-7 下午2:07.
 * Copyright (c) 2016 SAIZ.CN. All rights reserved.
 */

package com.example.db.utils;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteCursor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public final class JdbcUtil {

    private JdbcUtil() {
    }

    public static Object getColumnValue(Cursor cursor, int index) {
        SQLiteCursor sc = (SQLiteCursor) cursor;
        int type = sc.getType(index);
        if (Cursor.FIELD_TYPE_FLOAT == type) {
            return sc.getFloat(index);
        } else if (Cursor.FIELD_TYPE_INTEGER == type) {
            return sc.getLong(index);
        } else if (Cursor.FIELD_TYPE_BLOB == type) {
            return sc.getBlob(index);
        } else {
            return sc.getString(index);
        }
    }

    public static String[] convertParams(ContentValues values) {
        if (values == null) {
            return null;
        }
        List<String> arr = new ArrayList<String>();
        Set<Map.Entry<String, Object>> mes = values.valueSet();
        for (Map.Entry<String, Object> me : mes) {
            arr.add(String.valueOf(me.getValue()));
        }
        return arr.toArray(new String[arr.size()]);
    }

    public static String in(String splitParams) {
        String[] params = splitParams.split(",");
        if (ObjectUtil.isEmpty(params)) {
            return null;
        }
        StringBuffer sb = new StringBuffer();
        sb.append("(");
        for (int i = 0, len = params.length; i < len; i++) {
            sb.append("'").append(params[i]).append("'");
            if (i < len - 1) {
                sb.append(",");
            }
        }
        sb.append(")");
        return sb.toString();
    }
}
